package org.example.core;

import lombok.Data;
import org.example.common.Config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Data
public class H2Config {

    private String driverClass;
    private String jdbcUrl;
    private String username;
    private String password;

    public static H2Config getInstance(){
        H2Config config = new H2Config();
        config.setDriverClass(Config.get("spring.h2.driverClass"));
        config.setJdbcUrl(Config.get("spring.h2.jdbcUrl"));
        config.setUsername(Config.get("spring.h2.username"));
        config.setPassword(Config.get("spring.h2.password"));
        return config;
    }

    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        H2Config config = getInstance();
        Class.forName(config.getDriverClass());
        return DriverManager.getConnection(config.getJdbcUrl(),config.getUsername(),config.getPassword());
    }
}
